<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Authentication extends CI_Controller {
    function __construct()
    {
      parent::__construct();
      $this->load->library('session');
      $this->load->model('auth_user');
    }
   
    public function index()
    {
        $user = $this->session->userdata('user');
        if ( !empty($user) )
            redirect();
    }
    
    public function auth() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user   = $this->auth_user->get_user($username,$password);
        if (!empty($user)) {
			
            $sessiondata = array(
                'id' => $user->id,
                'username' => $user->username,
                'password'=>$user->password,
                'images'=>$user->images,
            );
            $this->session->set_userdata('user', $sessiondata);
			$this->session->set_userdata('user_authentication', $sessiondata);
            redirect('home');
        } else {
            redirect('homepage');
        }
    }
    
    public function logout() {
        $this->session->unset_userdata('user');
        redirect('homepage');
    }
}
?>